unit uRegFunction;

interface

uses windows, classes, sysutils;

procedure PerlRegMatch(const subject, SRegEx: string; results: TStrings; BoUnGreedy: boolean = false);
function PerlRegFind(const SrcStr, SRegX: string; BoUnGreedy: boolean = false): boolean;
function PerlRegMatchMustOnlyOne(const subject, SRegEx: string; var Str: string; BoUnGreedy: boolean = false): boolean;
function PerlRegMatchStr(const subject, SRegEx: string; BoUnGreedy: boolean = false): string;

implementation

uses PerlRegEx;

procedure PerlRegMatch(const subject, SRegEx: string; results: TStrings; BoUnGreedy: boolean = false);
var
  reg               : TPerlRegEx;
begin
  reg := TPerlRegEx.Create;
  try
    reg.Options := reg.Options + [preCaseLess];
    if BoUnGreedy then
      reg.Options := reg.Options + [preUnGreedy];
    reg.Subject := subject;
    reg.RegEx := SRegEx;
    if reg.Match then
    begin
      repeat
        results.Add(reg.MatchedText);
      until not reg.MatchAgain;
    end;
  finally
    reg.Free;
  end;
end;

function PerlRegFind(const SrcStr, SRegX: string; BoUnGreedy: boolean = false): boolean;
var
  reg               : TPerlRegEx;
begin
  reg := TPerlRegEx.Create;
  try
    reg.Options := reg.Options + [preCaseLess];
    if BoUnGreedy then
      reg.Options := reg.Options + [preUnGreedy];
    reg.Subject := SrcStr;
    reg.RegEx := SRegX;
    result := reg.Match;
  finally
    reg.Free;
  end;
end;

function PerlRegMatchMustOnlyOne(const subject, SRegEx: string; var Str: string; BoUnGreedy: boolean = false): boolean;
var
  reg               : TPerlRegEx;
begin
  Result := false;
  reg := TPerlRegEx.Create;
  try
    reg.Options := reg.Options + [preCaseLess];
    if BoUnGreedy then
      reg.Options := reg.Options + [preUnGreedy];
    reg.Subject := subject;
    reg.RegEx := SRegEx;
    if reg.Match then
    begin
      Str := reg.MatchedText;
      if reg.MatchAgain then
      begin
        Result := false;
        Exit;
      end
      else
        Result := true;
    end;
  finally
    reg.Free;
  end;
end;

function PerlRegMatchStr(const subject, SRegEx: string; BoUnGreedy: boolean = false): string;
var
 reg:TPerlRegEx;
begin
  Result := '';
  reg := TPerlRegEx.Create;
  try
    reg.Options := reg.Options + [preCaseLess];
    if BoUnGreedy then
      reg.Options := reg.Options + [preUnGreedy];
    reg.Subject := subject;
    reg.RegEx := SRegEx;
    if reg.Match then
      Result := reg.MatchedText;
  finally
    reg.Free;
  end;
end;

end.

